<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/main.css" >
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js" integrity="sha256-NGC9JEuTWN4GhTj091wctgjzftr+8WNDmw0H8J5YPYE=" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/gh/InventivetalentDev/MineRender@1.4.6/dist/skin.min.js"></script>
    </head>
    <body>

        <div class="main">
            <div class="skin-container" id=skin1 ></div>
            
        </div>
        
        <div class="main">
            <div class="skin-container" id=skin2 ></div>
        </div>



        <?php
        $playername=htmlspecialchars($_GET['playername']);

        if(!$playername) die("no playername");


        $json_uuid=json_decode(file_get_contents("https://api.mojang.com/users/profiles/minecraft/{$playername}"),true);
        //echo "https://api.mojang.com/users/profiles/minecraft/{$playername}";
        if(!$json_uuid) die("no json_uuid");
        

        $json_textures=json_decode(file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/{$json_uuid["id"]}"),true);
        //echo "https://sessionserver.mojang.com/session/minecraft/profile/{$json_uuid["id"]}";
        if(!$json_textures) die("no json_textures");


        //echo var_dump($json_textures);

        $vaule_obj=$json_textures['properties'][0]['value'];
        if(!$json_textures) die("no json_textures");

        
        

        //['properties'][0]['name']
        
        //echo $vaule_obj;

        $value_decoded=json_decode(base64_decode($vaule_obj),true);
        
        $skin_url=$value_decoded["textures"]["SKIN"]["url"];
        $cape_url=$value_decoded["textures"]["CAPE"]["url"];
        $is_slim=$value_decoded["textures"]["SKIN"]["metadata"]["model"]!=null;

        echo $skin_url;


        https://api.mojang.com/users/profiles/minecraft/<玩家名称>


        function my_exit(string $str)
        {
            echo $str;
            header("Location: change_page.php");
        }
        ?>
    </body>


    <script>
        function renderSkin(containerId, skinUrl,capeUrl,slim=false) {
                var container = document.getElementById(containerId);
                var skinRender = new SkinRender({
                    autoResize: true,
                    render: {
                        taa: true,
                        
                    }
                }, container);

                if(capeUrl=="")
                {
                    skinRender.render({
                    url: skinUrl,
                    optifine: true,
                    slim:slim
                    });
                }
                else
                {
                    skinRender.render({
                    url: skinUrl,
                    capeUrl: capeUrl,
                    optifine: true,
                    slim:slim
                    });
                }

                // 默认启用动画
                var animate = true;

                // 动画效果
                var startTime = Date.now();
                container.addEventListener("skinRender", function (e) {
                    if (animate) {
                        var t = (Date.now() - startTime) / 1000;
                        e.detail.playerModel.children[2].rotation.x = Math.sin(t * 5) / 2; // 左腿
                        e.detail.playerModel.children[3].rotation.x = -Math.sin(t * 5) / 2; // 右腿
                        e.detail.playerModel.children[4].rotation.x = Math.sin(t * 5) / 2; // 左臂
                        e.detail.playerModel.children[5].rotation.x = -Math.sin(t * 5) / 2; // 右臂
                    }
                });


                var k=0.43;
                container.addEventListener("skinRender", function(e) {
                    // 设置缩放
                    e.detail.playerModel.scale.set(k,k,k); // 将模型缩小为原来的50%
                    
                    // 设置位置
                    e.detail.playerModel.position.set(0, 10.1, 0); // 调整位置以使模型在容器内居中
                });
            }

            var skinsData = {
            1: "<?php echo $skin_url; ?>",
            2: "<?php echo $cape_url; ?>",
            3: "<?php echo $is_slim; ?>"
            
            };

            
            renderSkin("skin1", skinsData[1],"",skinsData[3]);
            renderSkin("skin2", skinsData[1],skinsData[2],skinsData[3]);

            
            

            
            



    </script>
</html>
